package com.bdqn.qnnews.minio.config;

import com.bdqn.qnnews.minio.utils.MinioUtil;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xlzhang
 * @date 2023/12/23 13:46
 */
@Configuration
@ConditionalOnClass(MinioClient.class)
@EnableConfigurationProperties(MinioQnProperties.class)
public class MinioAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(MinioClient.class)
    public MinioClient minioClient(MinioQnProperties prop){
        // 创建一个客户端对象, endpoint: minio服务器连接地址
        return MinioClient.builder()
                .endpoint(prop.getEndpoint())
                .credentials(prop.getAccessKey(), prop.getSecretKey())
                .build();
    }

    @Bean
    @ConditionalOnMissingBean(MinioUtil.class)
    public MinioUtil minioUtil(MinioClient minioClient, MinioQnProperties prop){
        return new MinioUtil(minioClient, prop);
    }

}
